home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Languages / Masm V6.11 / SAMPLES / NTSAMPLE / THREADS / ASMTHRED.AS$ / ASMTHRED.bin
Encoding:
Text File  |  1993-09-21  |  4.5 KB  |  176 lines

  1. ;----------------------------------------------------------------------
  2. ;
  3. ;  FUNCTION:  AsmThreadProc (LPVOID)
  4. ;
  5. ;  PURPOSE:   A thread procedure which calculates position on the window
  6. ;             and draws a colored rectangle.  The color of the rectangle
  7. ;             is determined by the input parameter.
  8. ;
  9. ;  VARIABLES USED:
  10. ;
  11. ;    - horizontal, vertical:
  12. ;             Local integers used to indicate the next directional move the
  13. ;             rectangle will make.
  14. ;
  15. ;    - ulx, uly, Locals which are moved to ESI and EDI as an optimization.
  16. ;             Integers used for the Upper Left X corner and Upper
  17. ;             Upper Left Y position of the rectangle.
  18. ;
  19. ;    - rect:  A Local RECT structure used to determine the current size of 
  20. ;             the window (in case the user resizes it).
  21. ;
  22. ;    - hdc:   Local variable, HDC of the rectangle.
  23. ;
  24. ;    - Time:  A Local _SYSTEMTIME structure.  It's milli-second field is 
  25. ;             used to create an apparent random starting point for the
  26. ;             rectangles.
  27. ;
  28. ;    - hBrush:A Local handle to a Brush object, used to set the color of 
  29. ;             the rectangle.
  30. ;
  31. ;    - wdth,  height:
  32. ;             Local Integers used for the width and height of the rectangles.
  33. ;
  34. ;     - hWind: Handle to a window that's shared with the main module, so that 
  35. ;             this procedure can draw to a window. External variable.
  36. ;
  37. ;     - lpColor: Long pointer parameter that holds the address of the color of 
  38. ;               the bouncing rectangle. Passed from the main module.
  39. ;
  40. ;  CALLED BY:
  41. ;
  42. ;    MainWndProc();
  43. ;
  44. ;----------------------------------------------------------------------
  45.  
  46.  
  47.  
  48. .386                            ; .386 before .model, thus 32bit segments.
  49. .model flat, C
  50.  
  51.  
  52. .data
  53. include asmthred.inc            ; contains structures, variables, etc.
  54.  
  55. externdef hWind:dword            ; Handle to a window shared with main the module so 
  56.                                 ; that the Assembly procedure can draw to the window.
  57.  
  58.  
  59. AsmThreadProc PROTO C, lpColor:LPVOID
  60.  
  61.  
  62.  
  63. .code
  64.  
  65.  
  66. AsmThreadProc PROC, lpColor:LPVOID
  67.  
  68. LOCAL   horizontal:dword, vertical:dword, rect1:RECT, handleDC:HDC, 
  69.         Time:_SYSTEMTIME, hBrush:HANDLE, wdth:dword, height:dword
  70.  
  71.                 
  72.     mov        wdth, 20                                ; Height of box
  73.     mov        height, 20                                ; Width of box.
  74.         
  75.     invoke    GetSystemTime, addr Time                ; Get the time
  76.  
  77.     invoke GetClientRect, hWind, addr rect1            ; Loop making sure the window exists.
  78.     .while ( ! eax ) 
  79.     invoke GetClientRect, hWind, addr rect1
  80.     .endw
  81.  
  82.  
  83.     xor        eax, eax                                ; Use Mod to get 
  84.     mov        ax, word ptr Time.wMilliseconds            ; random X position
  85.     cdq
  86.     idiv    dword ptr rect1.right
  87.     mov        esi, edx                                ; X position is in ESI.
  88.                     
  89.  
  90.     xor        eax, eax                                ; Use Mod to get 
  91.     mov        ax, word ptr Time.wMilliseconds            ; random Y position
  92.     cdq
  93.     idiv    dword ptr rect1.bottom
  94.     mov        edi, edx                                ; Y position is in EDI.
  95.  
  96.  
  97.     xor        eax, eax                                ; Use MOD to pick random direction.
  98.     mov        ax, time.wMilliseconds                    
  99.     cdq
  100.     mov        ecx, 02h
  101.     idiv    ecx
  102.  
  103.  
  104.     mov horizontal, 1        
  105.     .if ( edx == 0 )                                 
  106.         mov    vertical, 1
  107.     .else        
  108.         mov    vertical, -1                        
  109.     .endif
  110.  
  111.     
  112.     mov        ebx, lpColor
  113.     mov        eax, [ebx]                                    ; Set color as per 
  114.     invoke    CreateSolidBrush, eax                        ; input parameter
  115.     mov        hBrush, eax
  116.  
  117.  
  118.     .while ( 1 )                                         ; Do Forever..
  119.  
  120.         invoke GetClientRect, hWind, addr rect1
  121.         
  122.         mov eax, wdth
  123.         add    eax, esi
  124.         .if ( eax > rect1.right )                         ; check for right edge.
  125.             mov    esi, rect1.right                        ; if so change direction.
  126.             sub    esi, wdth                                                            
  127.             mov    horizontal, -1    
  128.         .endif
  129.  
  130.  
  131.         mov    eax, height
  132.         add eax, edi
  133.         .if ( eax > rect1.bottom )                        ; check for bottom edge
  134.             mov    edi, rect1.bottom                        ; if so change direction.
  135.             sub    edi, height            
  136.             mov    vertical, -1
  137.         .endif
  138.  
  139.  
  140.         .if ( edi <= 1 )                                 ; check for left edge
  141.             mov    edi, 1                                    ; if so change direction.
  142.             mov vertical, 1
  143.         .endif
  144.  
  145.         
  146.         .if    ( esi <= 1 )                                 ; check for top edge.
  147.             mov    esi, 1                                    ; if so change direction.
  148.             mov    horizontal, 1                  
  149.         .endif
  150.  
  151.  
  152.         invoke     GetDC, hWind                            ; get handle to DC
  153.         mov        handleDC, eax
  154.  
  155.         invoke    SelectObject, handleDC, hBrush            ; set brush color
  156.  
  157.  
  158.         mov        eax, height
  159.         add        eax, edi
  160.         mov        ebx, wdth
  161.         add        ebx, esi
  162.         invoke    Rectangle, handleDC, esi, edi, ebx, eax ; draws a rectangle
  163.         
  164.         invoke    ReleaseDC, hWind, handleDC                ; release DC
  165.         
  166.         add    esi, horizontal                                ; increment the X and
  167.         add    edi, vertical                                ; Y positions.
  168.  
  169.     .endw
  170.     
  171.     ret    
  172.  
  173. AsmThreadProc endp
  174.  
  175. end
  176.